namespace mplat {
    export class OppoNativeAd extends NativeAd {

        protected _nativeAd: any;
        protected _nativeAdData: any;

        protected _successFun: (res) => void;
        protected _failFun: () => void;

        protected _isIntervaling = false;// 是否在间隔中

        /** 这个数据，开发者自己要在自己的业务上处理本地缓存，然后将正确的值传入 */
        setInfo(info: INativeInfo) {
            this._info = info;
            if (isNaN(this._info.intervalTime)) this._info.intervalTime = 60;// 默认60s
            if (isNaN(this._info.errorClickRate)) this._info.errorClickRate = 0;// 默认0
            if (isNaN(this._info.errorClickMaxCount)) this._info.errorClickMaxCount = 0;// 默认0
            if (isNaN(this._info.daliyErrorClickMaxCount)) this._info.daliyErrorClickMaxCount = 0;// 默认0
            if (isNaN(this._info.delayShow)) this._info.delayShow = 0;// 默认0s
            if (isNaN(this._info.maxTimes)) this._info.maxTimes = 8;// 默认8
            if (isNaN(this._info.daliyMaxTimes)) this._info.daliyMaxTimes = 8;// 默认8
            if (!this._info.beforeLoadDestroy) this._info.beforeLoadDestroy = false;
        }

        loadNativeAd(success?: (res: any) => void, fail?: () => void, adId?: string | number) {
            if (plat.ad.state == AdState.NoAd) {
                debugLog('广告状态设置为了未上线状态，所以不显示')
                if (!!fail) fail();
                return;
            }
            if (!qg.createNativeAd) {
                debugLog('当前平台不支持原生广告');
                return;
            }
            if (!this._info || !this._info.ids || !this._info.ids.length) {
                debugLog('未设置原生广告数据');
                return;
            }
            if (this._info.maxTimes <= 0 || this._info.daliyMaxTimes <= 0) {
                debugLog('次数到上限了，不显示原生广告');
                return;
            }
            if (this._isIntervaling) {
                debugLog('距离上次原生拉取时间太近，还在cd中');
                return;
            }
            let realAdId = this._info.ids[0];
            if (typeof adId == 'number') {
                realAdId = this._info.ids[adId] || this._info.ids[0];
            } else {
                if (!!adId) {
                    realAdId = adId;
                }
            }
            this._successFun = success;
            this._failFun = fail;

            if (this._nativeAd && this._info.beforeLoadDestroy) {
                this.destroy();
            }
            this._nativeAd = this._getAd(realAdId);
            this._nativeAd.load();

            this._isIntervaling = true;// 设置在拉取间隔中
            timerOnce(this._info.intervalTime * 1000, this, () => {
                this._isIntervaling = false;
            })
        }

        reportAdClick(adId?: any) {
            if (!this._info || !this._info.ids || !this._info.ids.length) {
                debugLog('未设置原生广告数据')
                return;
            }
            if (!this._nativeAd || !this._nativeAdData) return;
            adId = adId || this._nativeAdData.adId;
            console.log('点击原生', adId);
            this._nativeAd.reportAdClick({ adId: adId.toString() });
        }

        destroy() {
            if (!this._nativeAd) return;
            this._nativeAd.offError(this._onErr.bind(this))
            this._nativeAd.offLoad(this._onLoaded.bind(this))
            if (!!this._nativeAd['destroy']) this._nativeAd['destroy']()
            this._nativeAd = null;
            this._nativeAdData = null;
            this._successFun = null;
            this._failFun = null;
        }

        closeNativeAd(adId?: any) {
            if (!!this._info.closeNativeAd) this._info.closeNativeAd();
            if (this._info.errorClickMaxCount > 0 && this._info.daliyErrorClickMaxCount > 0 && Math.random() < this._info.errorClickRate) {
                // 触发误触
                this._info.errorClickMaxCount--;
                this._info.daliyErrorClickMaxCount--;
                this.reportAdClick(adId)
            }
        }

        protected _getAd(adId?: string) {
            if (!this._nativeAd) {
                this._nativeAd = qg.createNativeAd({
                    adUnitId: adId
                })
                this._nativeAd.onError(this._onErr.bind(this))
                this._nativeAd.onLoad(this._onLoaded.bind(this))
            }
            return this._nativeAd;
        }

        protected _onErr(err) {
            debugLog('native err', err);
            if (!!this._info.onErr) this._info.onErr(err);
            if (!!this._failFun) this._failFun();
            this._successFun = null;
            this._failFun = null;
        }
        protected _onLoaded(res) {
            debugLog('native loaded', JSON.stringify(res))
            if (!!this._info.loadSuccess) this._info.loadSuccess();
            if (res && res.adList) {
                let adInfo = res.adList.pop();
                this._nativeAdData = adInfo;
                this._nativeAd.reportAdShow({ adId: adInfo.adId.toString() });
                if (this._info.delayShow > 0) {
                    timerOnce(this._info.delayShow * 1000, this, () => {
                        if (!!this._successFun) this._successFun(adInfo);
                        this._successFun = null;
                        this._failFun = null;
                    })
                } else {
                    if (!!this._successFun) this._successFun(adInfo);
                    this._successFun = null;
                    this._failFun = null;
                }

                if (this._info.maxTimes > 0) this._info.maxTimes--;
                if (this._info.daliyMaxTimes > 0) this._info.daliyMaxTimes--;
            }
        }
    }
}